<template>
  <div>
    <!-- 返利红票审批 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <FilterTable
      ref="table"
      columnsRef="qjhRebateRedInvoiceApprove"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" @click="addHandle(1)" :disabled="isOpen">审批通过</multiuse-button>
        <multiuse-button size="small" type="primary" @click="addHandle(2)" :disabled="isOpen">审批驳回</multiuse-button>
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="isDealer" title="经销商查询" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <SearchDealer :rowData="formData" @close="emitCloseHandler1" />
    </BaseDialog>
    <Drawer :visible.sync="visible" :title="'详情'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="72%">
      <SonModel :rowData="formData" @close="emitcloseHandler" />
    </Drawer>
    <BaseDialog :visible.sync="isreject" :title="'驳回意见'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <reject :list="selectedList" @close="rejectclose" />
    </BaseDialog>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { queryList, batchAudit } from '@/api/omFinance/RebateRedInvoice/RebateRedInvoiceApprove';
import SonModel from './SonModel';
import SearchDealer from './ownerSeacher';
import reject from './reject';
import { isArcfox } from '@/api/cms-api/origin/commonDealerType';

export default {
  name: 'RebateRedInvoiceApprove',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchParams: {
        searchType: 2
      },
      fetchApi: queryList,
      formData: {},
      visible: false,
      isreject: false,
      isDealer: false,
      isOpen: true,
      selectedList: []
    };
  },
  components: {
    SonModel,
    SearchDealer,
    reject
  },
  mixins: [dictionary],
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    if (isArcfox()) {
      this.columns.find(x => x.dataIndex == 'salesOrgCode').editItems = [{ text: '北京蓝谷极狐投资管理有限公司销售组织', value: '1012' }];
      this.columns.find(x => x.dataIndex == 'salesOrgCode').filterItems = [{ text: '北京蓝谷极狐投资管理有限公司销售组织', value: '1012' }];
    } else {
      this.columns.find(x => x.dataIndex == 'salesOrgCode').editItems = [
        { text: '北京汽车销售有限公司', value: '1080' },
        { text: '株洲北汽汽车销售有限公司', value: '1180' },
        { text: '北京新能源汽车营销有限公司', value: '2000' },
        { text: '青岛卫蓝销售公司', value: '2001' }
      ];
      this.columns.find(x => x.dataIndex == 'salesOrgCode').filterItems = [
        { text: '北京汽车销售有限公司', value: '1080' },
        { text: '株洲北汽汽车销售有限公司', value: '1180' },
        { text: '北京新能源汽车营销有限公司', value: '2000' },
        { text: '青岛卫蓝销售公司', value: '2001' }
      ];
    }
  },
  methods: {
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.dealerHandle()}></el-button>
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请输入'
        },
        // {
        //   type: 'SELECT',
        //   label: '状态',
        //   fieldName: 'applyStatus',
        //   placeholder: '请选择',
        //   itemList: this.createDictList('6021')
        // },
        {
          type: 'INPUT',
          label: '申请人',
          fieldName: 'applyPersonName',
          placeholder: '请输入'
        },
        {
          type: 'RANGE_DATE',
          label: '申请日期',
          fieldName: 'beginApplyDate|endApplyDate',
          dateType: 'exactdaterange',
          placeholder: '请选择',
          initialValue: []
        },
        {
          type: 'INPUT',
          label: '申请单号',
          fieldName: 'applyNo',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '动力形式',
          fieldName: 'powerType',
          placeholder: '请选择',
          itemList: this.createDictList('6008')
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 60,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 50,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row)}>
                  编辑
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '状态',
          dataIndex: 'applyStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6021'),
          render: p => {
            return <span>{this.createDictText(p.row.applyStatus, '6021')}</span>;
          }
        },
        {
          title: '申请单号',
          dataIndex: 'applyNo',
          width: 200,
          align: 'center',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '动力形式',
          dataIndex: 'powerType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6008'),
          render: p => {
            return <span>{this.createDictText(p.row.powerType, '6008')}</span>;
          }
        },
        {
          title: '公司代码',
          dataIndex: 'salesOrgCode',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [],
          editItems: []
        },
        {
          title: '返利类型',
          dataIndex: 'rebateType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6036'),
          render: p => {
            return <span>{this.createDictText(p.row.rebateType, '6036')}</span>;
          }
        },
        {
          title: '总申请金额',
          dataIndex: 'totalApplyAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },
        {
          title: '申请日期',
          dataIndex: 'applyDate',
          dateFormat: 'yyyy-MM-dd',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '申请人',
          dataIndex: 'applyPersonName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '税率1(%)',
          dataIndex: 'taxRate1',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '金额1（含税）',
          dataIndex: 'includeTaxAmount1',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },
        {
          title: '税额1',
          dataIndex: 'taxAmount1',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },
        {
          title: '不含税金额1',
          dataIndex: 'outcluedeTaxAmount1',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },

        {
          title: '税率2(%)',
          dataIndex: 'taxRate2',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '金额2（含税）',
          dataIndex: 'includeTaxAmount2',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },
        {
          title: '税额2',
          dataIndex: 'taxAmount2',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },
        {
          title: '不含税金额2',
          dataIndex: 'outcluedeTaxAmount2',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },
        {
          title: '返利类型_1',
          dataIndex: 'rebateTypeD',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6036'),
          render: p => {
            return <span>{this.createDictText(p.row.rebateTypeD, '6036')}</span>;
          }
        },
        {
          title: '总申请金额_1',
          dataIndex: 'totalApplyAmountD',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },
        {
          title: '税率1_1(%)',
          dataIndex: 'taxRate1D',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '金额1_1（含税）',
          dataIndex: 'includeTaxAmount1D',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },
        {
          title: '税额1_1',
          dataIndex: 'taxAmount1D',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },
        {
          title: '不含税金额1_1',
          dataIndex: 'outcluedeTaxAmount1D',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },

        {
          title: '税率2_1(%)',
          dataIndex: 'taxRate2D',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '金额2_1（含税）',
          dataIndex: 'includeTaxAmount2D',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },
        {
          title: '税额2_1',
          dataIndex: 'taxAmount2D',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },
        {
          title: '不含税金额2_1',
          dataIndex: 'outcluedeTaxAmount2D',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },
        {
          title: '红字发票信息表编号',
          dataIndex: 'remark',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    dealerHandle() {
      this.isDealer = true;
    },
    selectChangeHandle(row) {
      if (row.length > 0) {
        this.isOpen = false;
        this.selectedList = row || [];
      } else {
        this.isOpen = true;
        this.selectedList = [];
      }
    },
    emitCloseHandler1(val, data) {
      this.isDealer = false;
      let dealerCodes = [];
      if (data) {
        data.map(x => {
          dealerCodes.push(x.dealerCode);
        });
        this.topFilterList.find(x => x.fieldName == 'dealerCode').initialValue = dealerCodes.toString();
      }
    },
    rejectclose(val) {
      if (val) {
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
      this.isreject = false;
    },
    async addHandle(type) {
      if (type == 1) {
        await this.$confirm('是否通过?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            let arr = [];
            this.selectedList.map(x => {
              arr.push(x.id);
            });
            let param = {
              auditType: 1,
              rebateRedApplyIds: arr
            };
            const res = await batchAudit(param);
            if (res.resultCode == 200) {
              this.$notify({
                title: '成功',
                message: '通过成功',
                type: 'success'
              });
              this.fetchParams = { ...this.fetchParams };
            }
          })
          .catch(() => { });
      } else {
        this.isreject = true;
      }
    },
    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    editHandle(data) {
      this.visible = true;
      this.formData = data;
    },
    emitcloseHandler(type) {
      this.visible = false;
      if (type == 'refresh') {
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    }
  }
};
</script>
